#include <StatMaint.h>

/**
 * @brief 构造函数
 */
StatMaint::StatMaint()
{
}

/**
 * @brief 析构函数
 */
StatMaint::~StatMaint()
{}
Q_INVOKABLE QVariantMap StatMaint::getAllStaticInfo()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select Sum(EquOperatingTime) EquOperatingTime,Sum(Syringe) Syringe,Sum(AirBottle) AirBottle,Sum(Turntable) Turntable,Sum(RubberPad) RubberPad From DataStatistics";
    query.exec(sql);
    QVariantMap map;
    while (query.next()) {
        for (int i = 0; i < query.record().count(); ++i) {
            if(query.record().fieldName(i)=="EquOperatingTime"){
                map[query.record().fieldName(i)] = QString::number(query.value(i).toDouble(), 'f', 2);
            }else{
                map[query.record().fieldName(i)] = query.value(i);
            }
        }
    }
    return map;

}


Q_INVOKABLE QStringList StatMaint::getStaticInfoByDate(QString beginDate,QString endDate){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Sum(Syringe) Syringe,Sum(AirBottle) AirBottle,Sum(Turntable) Turntable,Sum(RubberPad) RubberPad "
                        "From DataStatistics "
                        "Where StaticDate>='%1' and StaticDate<='%2'").arg(beginDate,endDate);
    query.exec(sql);
    QStringList result;
    while (query.next()) {

        for (int i = 0; i < query.record().count(); ++i) {
            result.append(query.value(i).toString());
        }
    }
    return result;
}

Q_INVOKABLE int StatMaint::getMaxY(QString beginDate, QString endDate)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Sum(Syringe) Syringe,Sum(AirBottle) AirBottle,Sum(Turntable) Turntable,Sum(RubberPad) RubberPad "
                        "From DataStatistics "
                        "Where StaticDate>='%1' and StaticDate<='%2'").arg(beginDate,endDate);
    query.exec(sql);

    int result=0;
    while (query.next()) {

        for (int i = 0; i < query.record().count(); ++i) {
            int data=query.value(i).toInt();
            if(data>result){
                result=data;
            }
        }
    }
    return result+10;
}

Q_INVOKABLE QVariantMap StatMaint::getPercentageStaticInfo(QString beginDate,QString endDate)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Sum(Syringe) Syringe,Sum(AirBottle) AirBottle,"
                        "Sum(Turntable) Turntable,Sum(RubberPad) RubberPad "
                        "From DataStatistics "
                        "Where StaticDate>='%1' and StaticDate<='%2'").arg(beginDate,endDate);
    query.exec(sql);
    QVariantMap map;
    while (query.next()) {
        for (int i = 0; i < query.record().count(); ++i) {
            map[query.record().fieldName(i)] = query.value(i).toString().isNull()?0:query.value(i);
        }
    }
    int sum=map["Syringe"].toInt()+map["AirBottle"].toInt()+map["Turntable"].toInt()+map["RubberPad"].toInt();
    if(sum>0){
        map["Syringe"]= QString::number(map["Syringe"].toDouble()/sum*100, 'f', 2);
        map["AirBottle"]= QString::number(map["AirBottle"].toDouble()/sum*100, 'f', 2);
        map["Turntable"]=QString::number(map["Turntable"].toDouble()/sum*100, 'f', 2);
        map["RubberPad"]=QString::number(map["RubberPad"].toDouble()/sum*100, 'f', 2);
    }

    return map;
}

